const sass = require('sass')
// const mozjpeg = require('imagemin-mozjpeg');
// [imagemin.gifsicle(), imagemin.jpegtran(), imagemin.optipng(), imagemin.svgo()]
const loadGruntTasks = require('load-grunt-tasks')
const browserSync = require("browser-sync")
const bs = browserSync.create()
const data = {
    menus: [
        {
            name: 'Home',
            icon: 'aperture',
            link: 'index.html'
        },
        {
            name: 'Features',
            link: 'features.html'
        },
        {
            name: 'About',
            link: 'about.html'
        },
        {
            name: 'Contact',
            link: '#',
            children: [
                {
                    name: 'Twitter',
                    link: 'https://twitter.com/w_zce'
                },
                {
                    name: 'About',
                    link: 'https://weibo.com/zceme'
                },
                {
                    name: 'divider'
                },
                {
                    name: 'About',
                    link: 'https://github.com/zce'
                }
            ]
        }
    ],
    pkg: require('./package.json'),
    date: new Date()
}
module.exports = grunt => {
    grunt.initConfig({
        sass: {
            options: {
                sourceMap: true,
                implementation: sass
            },
            dist: {
                files: {
                    'temp/assets/styles/main.css': 'src/assets/styles/main.scss'
                }
            }
        },
        babel: {
            options: {
                sourceMap: true,
                presets: ['@babel/preset-env']
            },
            main: {
                files: {
                    'temp/assets/scripts/main.js': 'src/assets/scripts/main.js'
                }
            }
        },
        web_swig: {
            options: {
                swigOptions: {
                    cache: false
                },
                getData: function (tpl) {
                    return data;
                }
            },
            your_target: {
                // Target-specific file lists and/or options go here.
                expand: true,
                cwd: 'src/',//源文件文件夹
                src: "*.html",//后缀名匹配
                dest: "temp/"//目标文件夹
            },
        },
        // imagemin: {
        //     dist: {
        //         options: {
        //             optimizationLevel: 3,
        //         },
        //         files: [{
        //             expand: true,
        //             cwd: 'src/assets/images',
        //             src: ['*.*'],
        //             dest: 'dist/assets/images'
        //         }]
        //     }
        // },
        copy: {
            main: {
                files: [
                    { expand: true, cwd: 'src/assets/images', src: ['**'], dest: 'dist/assets/images/' },
                    { expand: true, cwd: 'public', src: ['**'], dest: 'dist' },
                    { expand: true, cwd: 'src/assets/fonts', src: ['**'], dest: 'dist/assets/fonts/' },
                    { expand: true, cwd: 'temp', src: ['*.html'], dest: 'dist' },
                ],
            },
        },
        //清除功能
        clean: {
            //所要清除的文件路径
            files: ['dist/**', 'temp/**']
        },
        //监视功能
        watch: {
            js: {
                //文件地址
                files: ['src/assets/scripts/*.js'],
                //执行的任务
                tasks: ['babel']
            },
            css: {
                //文件地址
                files: ['src/assets/style/*.scss'],
                //执行的任务
                tasks: ['sass']
            },
            html: {
                //文件地址
                files: ['src/**/*.html'],
                //执行的任务
                tasks: ['web_swig']
            },
            image: {
                //文件地址
                files: ['src/assets/images/*.*', 'src/assets/fonts/*.*', 'public/**/*.*'],
                //执行的任务
                tasks: ['copy']
            }
        },
        usemin: {
            html: ['dist/*.html'],
        },
        cssmin: {
            options: {
                ascii_only: true//中文ascii美化，防止中文乱码
            },
            my_target: {
                files: [
                    {
                        expand: true,//表示使用相对路径
                        cwd: "temp/assets/styles",//相对路径的根目录
                        src: "*.css",//相对路径下需要压缩的文件，*表示所有该后缀类型的文件，写具体的就是某个具体的文件将会被压缩
                        dest: "dist/assets/style"//压缩后的文件需要放置的目录，如果不存在的话，会自动创建
                    }
                ]
            }

        },
        uglify: {
            //当只有一个文件需要压缩时，可按下列方式书写代码
            //            build: {
            //                src: 'src/<%=pkg.file %>.js',
            //                dest: 'dest/<%= pkg.file %>.min.js'
            //            }
            my_target: {
                files: [
                    {
                        expand: true,//表示使用相对路径
                        cwd: "temp/assets/scripts",//相对路径的根目录
                        src: "*.js",//相对路径下需要压缩的文件，*表示所有该后缀类型的文件，写具体的就是某个具体的文件将会被压缩
                        dest: "dist/assets/scripts"//压缩后的文件需要放置的目录，如果不存在的话，会自动创建
                    }
                ]
            }
        },
        htmlmin: {
            options: {
                removeComments: true,
                removeCommentsFromCDATA: true,
                collapseWhitespace: true,
                collapseBooleanAttributes: true,
                removeAttributeQuotes: true,
                removeRedundantAttributes: true,
                useShortDoctype: true,
                removeEmptyAttributes: true,
                removeOptionalTags: true
            },
            html: {
                files: [
                    { expand: true, cwd: 'dist', src: ['*.html'], dest: 'dist' }
                ]
            }
        }
    })

    // grunt.loadNpmTasks('grunt-sass')
    // grunt.loadNpmTasks('grunt-babel')
    // grunt.loadNpmTasks('grunt-contrib-imagemin');
    loadGruntTasks(grunt)
    grunt.registerTask('bs-reload', function () {
        bs.reload()
    })

    grunt.registerTask('bs', function () {
        const done = this.async()
        bs.init({
            server: {
                port: 8080,
                // baseDir: 'dist',
                baseDir: ["temp", "public", "src"],
                files: 'temp/**',
                // open:false,
                routes: {
                    '/node_modules': "node_modules"
                }
            }
        })
    })

    grunt.registerTask('dev', ['sass', 'babel', 'web_swig', 'bs'])
    grunt.registerTask('build', ['clean', 'sass', 'babel', 'web_swig', 'copy', 'usemin', 'cssmin', 'uglify', 'htmlmin'])
}